﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dtos;
using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class TeachersBll:ITeachersBll
    {
        private ITeachersDal _T;
        public TeachersBll(ITeachersDal T) 
        {
        _T = T;
        }
        public async Task<int> AddTeachersAsync(string name, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, string pwd, string photo, Guid positionsId)
        {
            return await _T.AddAsync(new Model.Teachers
            {
                name = name,
                birthday = birthday,
                gender = gender,
                tel = tel,
                email = email,
                qqNumber = qqNumber,
                address = address,
                pwd = pwd,
                photo = photo,
                positionsId = positionsId
            });
        }

        public async Task<int> DeleteTeachersAsync(Guid id)
        {
            var data = await _T.GetIdAll(id);
            return data != null ? await _T.DeleteAsync(data) : 0;
        }

        public async Task<int> EditTeachersAsync(Guid id, string name, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, string pwd, string photo, Guid positionsId)
        {
            var data = await _T.GetIdAll(id);
            if (data == null)
                return 0;
            data.name = name;
            data.birthday = birthday;
            data.gender = gender;
            data.tel = tel;
            data.email = email;
            data.qqNumber = qqNumber;
            data.address = address;
            data.pwd = pwd;
            data.photo = photo;
            data.positionsId = positionsId;
            data.UpdateTime = DateTime.Now;
            return await _T.UpdateAsync(data);
        }

        public async Task<List<TeachersDtos>> getAllTeachers()
        {
            var data = await _T.GetAll();
            return data.Select(d => new TeachersDtos
            {
                Id = d.Id,
                name = d.name,
                birthday = d.birthday,
                gender = d.gender,
                tel = d.tel,
                email = d.email,
                qqNumber = d.qqNumber,
                address = d.address,
                pwd = d.pwd,
                photo = d.photo,
                positionsId = d.positionsId,
                UpdateTime = DateTime.Now
            }).OrderByDescending(d => d.UpdateTime).ToList();
        }

        public async Task<List<TeachersDtos>> getTeachersByNameAsync(string title)
        {
            var data = await _T.GetDataByChoose(a => a.name.Contains(title));
            return data == null ? null : data.Select(m => new TeachersDtos
            {
                Id = m.Id,
                name = m.name,
                birthday = m.birthday,
                gender = m.gender,
                tel = m.tel,
                email = m.email,
                qqNumber = m.qqNumber,
                address = m.address,
                pwd = m.pwd,
                photo = m.photo,
                positionsId = m.positionsId,
                UpdateTime = m.UpdateTime,
            }).OrderByDescending(g => g.UpdateTime).ToList();
        }

        public async Task<TeachersDtos> GetTeachersIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public async Task<bool> IsExistsAsync(string title)
        {
            var data = await _T.GetDataByChoose(m => m.name.Equals(title));
            return data.Count() > 0;
        }
    }
}
